#include "DataRefreshThread.h"
#include "../DopplerView/DopplerDataView.h"
#include <QtGlobal>
#include <QList>
#include <math.h>

volatile unsigned char  data[1024 * 3]  ;

static int a = 0 ;
void GenerateData()
{
    int i = 0 ;
    a+=5  ;
    for( i = 0 ; i < 3 * 1024 ; i++)
    {
        data[i] =  (unsigned char)(127 + 128 * sin( (i + a) / 60.0 ))  ;
    }

}

DataRefreshThread* DataRefreshThread::m_pThread =  new DataRefreshThread ;

DataRefreshThread* DataRefreshThread::Instance()
{
    if (!m_pThread)
    {
        m_pThread= new DataRefreshThread() ;
    }
    return m_pThread ;
}

DataRefreshThread::DataRefreshThread(QObject *parent) :
    QThread(parent)
{
    m_pList = new QList<DopplerDataView*> ;
    m_bStop = false ;
}

DataRefreshThread::~DataRefreshThread()
{
    delete m_pList ;
    m_pThread = NULL ;
}

void DataRefreshThread::StopThread(bool bStop_)
{
    //qDebug("stop thread");
    m_bStop = bStop_ ;
}


void DataRefreshThread::SetDrawList(QList<DopplerDataView*>* pList_)
{
    m_pList->clear();
    delete m_pList ;
    m_pList = pList_  ;
}

QList<DopplerDataView*>* DataRefreshThread::GetDrawList()
{
    return m_pList   ;
}

void DataRefreshThread::run()
{
    DopplerDataView* _pDraw ;
    int i  ;
    m_bStop = false ;
    while(!m_bStop)
    {
        GenerateData();
        if(m_pList&&m_pList->count())
        {
            for(i = 0 ;  i < m_pList->count() ; i++)
            {
                _pDraw = m_pList->at(i) ;
                _pDraw->UpdateDrawing();
            }
        }
        else
        {
            m_bStop = true ;
        }
        msleep(20) ;
    }
}
